package com.ll.xstream;

import java.io.Writer;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
import com.thoughtworks.xstream.io.json.JsonWriter;

/**
 * Created by daweiLiu on 2016/11/24.
 */
public class XstreamJson {
    public static void main(String[] args) {
        XStream xstream = new XStream(new JsonHierarchicalStreamDriver() {
            public HierarchicalStreamWriter createWriter(Writer writer) {
                return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE);
            }
        });
        Children children = new Children("Mahesh", "Parashar");
        xstream.setMode(XStream.NO_REFERENCES);
        xstream.alias("children", Children.class);
        System.out.println(xstream.toXML(children));

    }
}


@XStreamAlias("children")
class Children {

    private String firstName;
    private String lastName;

    public Children(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String toString() {
        return "Children [ firstName: " + firstName + ", lastName: " + lastName + " ]";
    }
}